// 对象有子对象，子对象有子对象……把这些所有对象放入一个数组中
const obj = {
    name: "1",
    child: {
        name: "2",
        child: {
            name: "3",
            child: {
                name: "4"
            }
        }
    },
    getArr(obj, arr) {
        if (!arr) {
            arr = [];
        }
        if (!obj) {
            obj = this;
            arr.push(this);
        }
        for (const key in obj) {
            if (key == 'getArr') {
                continue;
            }
            if (typeof obj[key] == 'object') {
                arr.push(obj[key]);
                this.getArr(obj[key], arr);
            }
        }
        return arr;
    }
}

console.log(obj.getArr());

// 数组排序
const arr = [-1, 3, 2, 5, 6, 10, 8];
// [-1 => 4,3 => 0,2 => 1,5 => 2,6 = > 3, 10 => 7,8 => 5]
// [3,2,5,6,-1,8,10]
arr.sort((a, b) => Math.abs(a - 3) - Math.abs(b - 3));
console.log(arr);

var foo = {n: 1};

(function(foo){
    foo.n = 3;
    foo = {n:2};
})(foo);

console.log(foo); // {n: 3}
